﻿#region

using System;

#endregion

namespace TSharp.Core.Message.MessageImpl
{
    /// <summary>
    ///     消息传递控制类
    ///     <para>by tangjingbo at 2009/12/14 21:37</para>
    /// </summary>
    /// <typeparam name="TMessage">The type of the message.</typeparam>
    internal class TransmitterImpl<TMessage> : ITransmitter<TMessage> 
    {
        public TransmitterImpl(WeakReference sender, TMessage msg)
        {
            this.sender = sender;
            message = msg;
        }

        #region IMessageTransmitter<TMessage> 成员

        private int maxDistance = int.MaxValue;

        public int MaxDistance
        {
            get { return maxDistance; }
            set { maxDistance = value; }
        }

        public void Terminate()
        {
            maxDistance = 0;
        }

        #endregion

        #region IMessageResult<TMessage> 成员

        private readonly TMessage message;
        private readonly WeakReference sender;

        public TMessage GetMessage()
        {
            return message;
        }

        public int GetListeneds { get; set; }

        public bool IsTerminated
        {
            get { return GetListeneds > MaxDistance; }
        }

        public object GetSender()
        {
            return sender.Target;
        }

        public IRegisterHandle<TMessage> RegHandle { get; internal set; }

        #endregion
    }
}